import CommonBaseSchema from '#common/base_schema'

export default class extends CommonBaseSchema {
  protected tableName = 'member_services'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments()
      table.integer('fk_shop_id').notNullable().unsigned().references('shops.id').comment('店铺ID')

      table
        .integer('fk_sales_order_id')
        .notNullable()
        .unsigned()
        .references('sales_orders.id')
        .comment('销售单ID')

      table.integer('fk_member_id').nullable().unsigned().references('members.id').comment('会员ID')

      table
        .integer('fk_rights_id')
        .notNullable()
        .unsigned()
        .references('service_rights.id')
        .comment('权益ID')

      table.string('name', 100).comment('名称')
      table.integer('actual_times').unsigned().defaultTo(0).comment('实际次数')
      table.integer('actual_gift_times').unsigned().defaultTo(0).comment('实际赠送次数')
      table.integer('actual_price').unsigned().defaultTo(0).comment('实际价格')
      table.integer('price').unsigned().defaultTo(0).comment('价格')
      table.integer('remain_amount').unsigned().defaultTo(0).comment('剩余金额')
      table.integer('remain_times').unsigned().defaultTo(0).comment('剩余次数')
      table.integer('remain_gift_times').unsigned().defaultTo(0).comment('剩余赠送次数')
      table.json('init_json').nullable().comment('初始 JSON 数据')
      table.dateTime('start_date').comment('开始日期')
      table.dateTime('end_date').nullable().comment('结束日期')
      table.integer('status').unsigned().defaultTo(1).comment('状态')
      table.string('memo', 255).defaultTo('').comment('备注')

      this.defineCommonColumns(table)
      table.comment('会员服务权益表')
    })
  }

  async down() {
    this.schema.dropTable(this.tableName)
  }
}
